package com.hns.can.master.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.master.entity.BaseCarDrvRelEntity;
import com.hns.can.master.entity.BaseCarInfoEntity;
import com.hns.can.master.entity.BaseDrvInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataUtil;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶员车辆分配的service
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class CarDrvRelService extends CommonService {
    
	/**
	 * 填充代码描述
	 */
    @Autowired
    private FillCodeDscValue codeDscValue;
    
   /**
    * 选中的车辆绑定驾驶员的情况列表，返回page对象
    * @param carIds
    * @return
    * @throws Exception
    * @author:lan
    * @email:763836737@qq.com
    */
    @SuppressWarnings({"rawtypes", "unchecked" })
    @LeftOrgAnnotation()
    @Transactional
    public Pagination searchBoundDrvPage(List<String> carIds) throws Exception {
        Pagination page = this.myBatisPage("carDrvRel.getBoundDrvListCnt","carDrvRel.getBoundDrvList",carIds,BaseCarDrvRelEntity.class);
        List list = page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);  
        FillParamsVo vo = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map2.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);   
        map2.put("workEmpeEmpno", "workEmpeEmpno");   
        map2.put("timecardCardno", "timecardCardno");   
        map2.put("gender", "gender"); 
        map2.put("ctcWay", "ctcWay"); 
        FillParamsVo vo2 = new FillParamsVo("drvId", map2);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO,GlobalConst.CACHE_KEY_BASE_DRV_INFO},vo,vo2);
        page.setData(list);
        return page;
    }
    
    /**
     * 取得一个机构下，启用状态，未被分配的驾驶员，返回page对象
     * @param organId
     * @param user
     * @param qdrvName
     * @param qworkEmpeEmpno
     * @param qtimecardCardno
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    @Transactional
    public Pagination getUnBoundDrvPage(String organId, SysUserEntity user, String qdrvName, 
        String qworkEmpeEmpno, String qtimecardCardno) throws Exception {
        String corpId = user.getCorpId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("corpId", corpId);
        map.put("organId", organId);
        map.put("qdrvName" , qdrvName);
        map.put("qworkEmpeEmpno", qworkEmpeEmpno);
        map.put("qtimecardCardno", qtimecardCardno);
        Pagination page = this.myBatisPage("carDrvRel.getUnBoundDrvListCnt","carDrvRel.getUnBoundDrvList",
            map,BaseDrvInfoEntity.class);
        List list = page.getData();
        Map<String,String> map5 = new HashMap<String,String>();
        map5.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo5 = new FillParamsVo("organId", map5);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_ORGAN}, vo5);
        page.setData(list);
        return page;
    }
    
    /**
     * 将驾驶员绑定到车辆上
     * @param carId
     * @param drvIds
     * @param user
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public void bindDrvsToCar(String carId, String[] drvIds, SysUserEntity user){
        BaseCarDrvRelEntity entity = null;
        for(String drvId : drvIds){
            if(getBoundCnt(drvId) > 0)
                throw new BusinessException(MsgType.ERROR.getFlag(), "该驾驶员已经绑定到车辆上了。");
            // 封装实体的数据
            entity = new BaseCarDrvRelEntity();
            entity.setCarId(carId);
            entity.setRelId(DataUtil.sequenceID());
            entity.setDrvId(drvId);
            entity.setCrtTime(new Date());
            entity.setCrtUser(user.getAcctName());
            entity.setCorpId(user.getCorpId());
            commonEntityDao.save(entity);
        }
    }
    
    /**
     * 解除驾驶员和车辆的绑定关系
     * @param relId
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public void unBindDrv(String relId){
        BaseCarDrvRelEntity entity = commonEntityDao.find(relId, BaseCarDrvRelEntity.class);
        commonEntityDao.delete(entity);
    }
   /**
    * 通过车辆id得到车辆信息
    * @param carId
    * @return
    * @author:lan
    * @email:763836737@qq.com
    */
    @Transactional
    public BaseCarInfoEntity getCarInfoByCarId(String carId){
        BaseCarInfoEntity entity = commonEntityDao.find(carId, BaseCarInfoEntity.class);
        return entity;
    }
    
   /**
    *  绑定前进行判断是否驾驶员已经绑定。
    * @param drvId
    * @return
    * @author:lan
    * @email:763836737@qq.com
    */
    public Integer getBoundCnt(String drvId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("drvId", drvId);
        List<BaseCarDrvRelEntity> list = commonEntityDao.findByParamsIgnoreNullValue(BaseCarDrvRelEntity.class, map);
        return list.size();
    }
    
    /**
     * 选中的车辆绑定驾驶员的情况列表
     * @param carIds
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"unchecked" })
    @LeftOrgAnnotation()
    @Transactional
    public List<BaseEntity> getBoundDrvList(List<String> carIds) throws Exception {
        List<BaseEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("carDrvRel.getBoundDrvList", carIds), BaseCarDrvRelEntity.class);
        Map<String,String> mapTmp = new HashMap<String,String>();
        mapTmp.put("gender", "SEX00000");
        codeDscValue.fillCodeDscValue(list, mapTmp);
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("carId", map);
        Map<String,String> map2 = new HashMap<String,String>();
        map.put("drvName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo2 = new FillParamsVo("drvId", map2);
        codeDscValue.fillInfoValue(list, new String[] {GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_CAR_INFO}, vo, vo2);
        return list;
    }
}
